# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import redis
from scrapy.exceptions import DropItem

redis_db = redis.Redis(host='127.0.0.1', port=6379, db=0) #连接redis，相当于MySQL的conn

class MonitorPublicOpinionPipeline(object):
    def process_item(self, item, spider):
        return item



class DuplicatesPipeline(object):
    """redis 去重url"""
    def process_item(self, item, spider):

        if hasattr(spider,'duplicates_model') and spider.duplicates_model:
            """模式1：重复字段title"""
            self.run_redis(item, 'crawl2018:dup_title','title')
        else:
            """模式0：重复字段href"""
            self.run_redis(item,"crawl2018:baidu_url",'href')
        return item

    def run_redis(self,item,redis_data_dict,field):
        if redis_db.hexists(redis_data_dict, item[field]):  # 取item里的url和key里的字段对比，看是否存在，存在就丢掉这个item。不存在返回item给后面的函数处理
            raise DropItem("Duplicate item found: %s" % item['title'])
        else:
            redis_db.hset(redis_data_dict, item[field], 0)



class SendEmailPipeline(object):
    def process_item(self, item, spider):
        # 第三方 SMTP 服务
        email_switch = redis_db.hget('crawl2018:config', 'email_switch').decode('utf-8', 'ignore')
        if email_switch == 'true':
            import smtplib
            from email.mime.text import MIMEText
            from email.header import Header
            mail_host = "smtp.mxhichina.com"  # 设置服务器
            mail_user = "insight7@gdinsight.com"  # 用户名
            mail_pass = "Group62606088"  # 口令
            sender = 'insight7@gdinsight.com'

            #获取抄送者
            rec_redis = redis_db.hgetall('crawl2018:email')
            if rec_redis:
                receivers = []
                for x in rec_redis:
                    receivers.append(rec_redis[x].decode('utf-8'))
            body = "关键词：%s\r\n标题：%s\r\n链接：%s\r\n位置：%s\r\n渠道网站：%s" % (item['keyword'],item['title'].strip(),item['href'],item['position'],item['channel'])
            subject = '舆情监控发现新的消息'
            message = MIMEText(body.encode('utf-8'), 'plain', 'utf-8')
            message['From'] = mail_user
            message['Subject'] = Header(subject, 'utf-8')
            message['To'] = ','.join(receivers)

            try:
                smtpObj = smtplib.SMTP_SSL(mail_host,465)
                smtpObj.ehlo()
                smtpObj.login(mail_user, mail_pass)
                smtpObj.sendmail(sender, receivers, message.as_string())
                smtpObj.close()
                print('邮件发送成功:'+','.join(receivers))
            except smtplib.SMTPException as e:
                print(e)
        return item

        # def email_send(self,item):
        #     mailer = MailSender(
        #         smtphost='smtp.mxhichina.com',
        #         mailfrom='sheyikai@gdinsight.com',
        #         smtpuser='sheyikai@gdinsight.com',
        #         smtppass='She5211314',
        #         smtpport=465,
        #         smtptls=False,
        #         smtpssl=True,
        #     )
        #     body = "关键词：%s\r\n标题：%s\r\n链接：%s\r\n位置：%s" % (item['keyword'],item['title'].strip(),item['href'],item['position'])
        #     #获取抄送者
        #     email_all = redis_db.hgetall('crawl2018:email')
        #     if email_all:
        #         copy_to = []
        #         for x in email_all:
        #             copy_to.append(email_all[x].decode('utf-8','ignore'))
        #
        #         send = mailer.send(
        #             to=["liuyang@gdinsight.com"],
        #             subject="舆情监控发现新的消息",
        #             body=body,
        #             cc=copy_to  # 抄送
        #         )
